package com.hiwan.unions.activity.main.homeInform;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.base.RootActivity;
import com.hiwan.unions.activity.main.homeInform.informFragment.AFragment;
import com.hiwan.unions.activity.main.homeInform.informFragment.BFragment;
import com.hiwan.unions.activity.main.homeInform.informFragment.CFragment;
import com.hiwan.unions.adapter.fragmentAdapters.MyFragmentPagerAdapter;
import com.hiwan.unions.util.SystemUtil;
import com.yanzhenjie.nohttp.Logger;

import java.lang.reflect.Field;
import java.util.ArrayList;

/**
 * Created by llg on 2017/7/8.
 */
public class InformActivity extends RootActivity {
    TabLayout tabLayout;
    ViewPager viewPager;
    int type = 0;
    private ArrayList<Fragment> listFragments;
    private String[] titles = new String[]{"时政要闻", "省级资讯", "市州资讯"};

    public static void startPreview(Context context, int type) {
        Intent intent = new Intent(context, InformActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }


    @Override
    protected int getLayout() {
        return R.layout.activity_tab_list;
    }

    @Override
    protected void initData() {
        bundle = getIntent().getExtras();
        type = bundle.getInt("type");
        Logger.i("type  " + type);
        tabLayout.getTabAt(type).select();
        viewPager.setCurrentItem(type);

    }

    @Override
    protected void onViewCreated() {
        super.onViewCreated();
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = findView(R.id.viewpager);
        listFragments = new ArrayList<Fragment>();
        listFragments.add(new AFragment());
        listFragments.add(new BFragment());
        listFragments.add(new CFragment());

        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), this, titles, listFragments);
        //viewpager加载adapter
        viewPager.setAdapter(adapter);
        //TabLayout加载viewpager
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        //设置欲缓存页
        viewPager.setOffscreenPageLimit(3);
        setTitleTxt("往期动态");
        //了解源码得知 线的宽度是根据 tabView的宽度来设置的
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //拿到tabLayout的mTabStrip属性
                    Field mTabStripField = tabLayout.getClass().getDeclaredField("mTabStrip");
                    mTabStripField.setAccessible(true);

                    LinearLayout mTabStrip = (LinearLayout) mTabStripField.get(tabLayout);

                    int dp10 = SystemUtil.dp2px(InformActivity.this, 10);

                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);
                        //拿到tabView的mTextView属性
                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                        mTextViewField.setAccessible(true);

                        TextView mTextView = (TextView) mTextViewField.get(tabView);

                        tabView.setPadding(0, 0, 0, 0);
                        //因为我想要的效果是   字多宽线就多宽，所以测量mTextView的宽度
                        int width = 0;
                        width = mTextView.getWidth();
                        if (width == 0) {
                            mTextView.measure(0, 0);
                            width = mTextView.getMeasuredWidth();
                        }
                        //设置tab左右间距为10dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                        params.width = width;
                        params.leftMargin = dp10;
                        params.rightMargin = dp10;
                        tabView.setLayoutParams(params);
                        tabView.invalidate();
                    }

                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
